<?php

	class EventNotificationDataAccessMySQL implements iEventNotificationDataAccess {
			
		function buildObject($result) {
			
			$notification = new Notification();
			
			$notification->setNotificationId($result->notificationId);
			$notification->setEventId($result->eventId);
			$notification->setEventItemId($result->eventItemId);
			$notification->setTitle($result->title);		
			$notification->setDescription($result->description);
			$notification->setNotificationDate(formatDate($result->notificationDate));
			
			$notificationUser = new EventUser();
			
			$user = new User();
			$user->setUserId($result->userId);
			$user->setName($result->name);
			
			$notificationUser->setUserRegistrationCode($result->registrationCode);
			$notificationUser->setUser($user);
			
			$notification->setNotificationUser($notificationUser);
			
			return $notification;
		}
		
		public function selectEventNotificationByEventId($eventId, $dataBaseConnection) {
			$resultArray = array();
			
			$parameters["EVENTID"]     = $eventId;
			$parameters["EVENTITEMID"] = "NULL";
			
			$query = buildQuery("querySelectNotification", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function selectEventNotificationByEventItemId($eventItemId, $dataBaseConnection) {
			$resultArray = array();
			
			$parameters["EVENTID"]     = "NULL";
			$parameters["EVENTITEMID"] = $eventItemId;
			
			$query = buildQuery("querySelectNotification", $parameters);
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
					while($row = mysql_fetch_object($result)) {
						array_push($resultArray, $this->buildObject($row));	
					}
					return $resultArray;	
				}
				else {
					return NULL;
				}
			} else {
				return NULL;
			}
		}
		
		public function saveNotification($notification, $dataBaseConnection) {
			$parameters = array();
			
			if ($notification->getEventId() != null) {
				$parameters["EVENTID"] = $notification->getEventId();
			}
			else {
				$parameters["EVENTID"] = "NULL";
			}
			
			if ($notification->getEventItemId() != null) {
				$parameters["EVENTITEMID"] = $notification->getEventItemId();
			}
			else {
				$parameters["EVENTITEMID"] = "NULL";
			}
			
			$parameters["REGISTRATIONCODE"] = $notification->getNotificationUser()->getUserRegistrationCode();
			$parameters["TITLE"] = "'" . $notification->getTitle() . "'";
			$parameters["DESCRIPTION"] = "'" . $notification->getDescription() . "'";
			$parameters["NOTIFICATIONDATE"] = "'" . formatDateMySQL($notification->getNotificationDate()) . "'";
			
			$query = buildQuery("queryInsertNotification", $parameters);
			
			if (!is_bool($query)) {
				if ($result = $dataBaseConnection->query($query)) {
						
					return $dataBaseConnection->getLastInsertId();
				}
				else {
					return -1;
				}
			} else {
				return -1;
			}
		}
	}

?>